package com.zhilianbao.leyaogo.ui.fragment.me.myaccount;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.model.response.me.RechargeSaleCard;
import com.zhilianbao.leyaogo.ui.activity.me.moneyaccount.CardVoucherActivity;
import com.zhilianbao.leyaogo.ui.adapter.me.RechargeSaleCardAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.RefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.Utils;

import java.util.List;

import okhttp3.Response;

/**
 * ClassName: RechargeSaleCardFragment <br>
 * Description: 电商线上可销售列表Fragment<br>
 * Author: Cyarie <br>
 * Created: 2017/3/8 09:45 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class RechargeSaleCardFragment extends RefreshAndLoadFragment<RechargeSaleCard> {

    //是否从首页进来
    private boolean isFromHome;

    private RechargeSaleCardAdapter mRechargeSaleCardAdapter;


    public static RechargeSaleCardFragment getInstance(Bundle bundle) {
        RechargeSaleCardFragment rechargeSaleCardFragment = new RechargeSaleCardFragment();
        rechargeSaleCardFragment.setArguments(bundle);
        return rechargeSaleCardFragment;
    }

    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public QuickRcvAdapter<RechargeSaleCard> getAdapter() {
        return mRechargeSaleCardAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(mActivity, 2);
    }

    @Override
    public void onRefresh() {
        loadDataList(1, true);
    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        if (Utils.getUserLoginInfo() != null) {
            AccountApi.getRechargeSaleCardList(mActivity, Utils.getUserLoginInfo().getUserId(), Utils.getSupplierId(), curPage, pageSize, new RefreshAndLoadCallback<List<RechargeSaleCard>>(isPullToRefresh) {
                @Override
                public void errorLeftOrEmptyBtnClick(View v) {
                    loadDataList(1, false);
                }

                @Override
                public void onResultSuccess(List<RechargeSaleCard> list, @Nullable Response response, LoadingViewCallback callback) {
                    handleRefreshAndLoadListData(curPage, callback, list);
                }
            });
        }

    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mRechargeSaleCardAdapter = new RechargeSaleCardAdapter(mActivity, mList);
        mRcvLoadMore.setPadding(0, Utils.dp2px(8), Utils.dp2px(4), Utils.dp2px(4));
        mRcvLoadMore.setClipToPadding(false);
    }

    @Override
    protected void initToolbarHere() {
        isFromHome = getArguments().getBoolean(IntentConfig.IS_FROM_HOME, false);
        if (isFromHome) {
            initToolbarWithRightText(getString(R.string.card_voucher), getString(R.string.my_voucher));
        } else {
            initToolbar(getString(R.string.card_voucher));
        }

    }


    @Override
    public void onLoadMore() {
        loadDataList(mCurPage, true);
    }

    @Override
    public void btnRightTextClick() {
        super.btnRightTextClick();
        Utils.startActivity(mActivity, CardVoucherActivity.class);
    }
}
